package imageViewHelper;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;

public class ImageViewResizeHandler {

	public static void resizeImageView(ImageView img, float ratio){
		LayoutParams imgLp = (LayoutParams) img.getLayoutParams();
		int height = img.getDrawable().getIntrinsicHeight();
		int width = img.getDrawable().getIntrinsicWidth();
		
        imgLp.height = (int)(height * ratio);
        imgLp.width = (int)(width * ratio);
        img.setLayoutParams(imgLp);
        
        
        Bitmap bitmap=((BitmapDrawable) img.getDrawable()).getBitmap();
		Matrix matrix = new Matrix();
		matrix.postScale(ratio, ratio);
        Bitmap resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        img.setImageDrawable(new BitmapDrawable(null, resultBitmap));
	}
	
	public static void resizeToFitTheView(ImageView img, View targetView){
		LayoutParams imgLp = (LayoutParams) img.getLayoutParams();
		int height = img.getDrawable().getIntrinsicHeight();
		int width = img.getDrawable().getIntrinsicWidth();
		
        imgLp.height = targetView.getHeight();
        imgLp.width = targetView.getWidth();
        img.setLayoutParams(imgLp);
        
        
        Bitmap bitmap=((BitmapDrawable) img.getDrawable()).getBitmap();
		Matrix matrix = new Matrix();
		matrix.postScale(width / imgLp.width, height / imgLp.height);
        Bitmap resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        img.setImageDrawable(new BitmapDrawable(null, resultBitmap));
	}
}
